var httpGoods = require('../../utils/goods-info.js');
var pageClass = require('../../library/page.js');

var config = new pageClass({
  count : 1,
  spec : null
});

/**
 * 页面数据载入
 */
config.onLoad = function(options){
  let http = new httpGoods;
  let self = this;
  http.success = function (data) {
    for(let x in data.specs)
      data.specs[x].selected = false;
    self.data.goods = data;
    self.setData(self.data);
  };
  http.send({
    id : options.id
  });
}

config.bindViewTap = function(){
  wx.navigateTo({
    url: '../logs/logs'
  });
};

/**
 * 选择Spec组件
 */
config.onSpecSelected = function(event){
  var index = parseInt(event.target.dataset.index);
  for (let x in this.data.goods.specs)
    this.data.goods.specs[x].selected = false;
  this.data.goods.specs[index].selected = true;
  this.data.spec = this.data.goods.specs[index];
  this.data.count = 1;
  this.setData(this.data);
}

/**
 * 增加数量事件
 */
config.onCountMinus = function(){
  if(this.data.spec==null) return;
  if(this.data.count<=1) return;
  this.data.count--;
  this.setData(this.data);
};

/**
 * 增加数量事件
 */
config.onCountPlus = function () {
  if (this.data.spec == null) return;
  if (this.data.count >= this.data.spec.stock) return;
  this.data.count++;
  this.setData(this.data);
};

config.onCountInput = function (event){
  let count = parseInt(event.detail.value);
  if (spec!=null && count >= 1 && count <= this.data.spec.stock){
    this.data.count = count;
    return event.detail.value;
  }
};

// 初始化页面
Page(config);
